package com.uwang.library.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.uwang.library.dao.IndexCountDao;
import com.uwang.library.dao.impl.IndexCountDaoImpl;
import com.uwang.library.service.IndexCountService;

public class IndexCountServiceImpl implements IndexCountService {
	
	@Override
	public Map<String, Object> getCount() {
		IndexCountDao indexCountDao = new IndexCountDaoImpl();
		Map<String, Object> map = new HashMap<>();
		List<Map> userTimeCountList = new ArrayList<>();
		List<Date> dateList = getListDateTime(new Date(), 7);
		for (Date date : dateList) {
			Map<String, String> dateNumMap = new HashMap<>();
			dateNumMap.put("dateTime", format(date));
			dateNumMap.put("countNum", String.valueOf(indexCountDao.getCount(date)));
			dateNumMap.put("getMemberCount", String.valueOf(indexCountDao.getMemberCount(date)));
			dateNumMap.put("getBookCount", String.valueOf(indexCountDao.getBookCount(date)));
			dateNumMap.put("getTimeoutCount", String.valueOf(indexCountDao.getTimeoutCount(date)));
			userTimeCountList.add(dateNumMap);
		}
		map.put("countNumber", userTimeCountList);
		return map;
	}

	private List<Date> getListDateTime(Date data, Integer num) {
        List<Date> timeKey = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(data);
        timeKey.add(data);
        for (int i = 0; i < num - 1; i++) {
            calendar.add(Calendar.DATE, -1);
            timeKey.add(calendar.getTime());
        }
        return timeKey;
    }
	
	public static String format(Date date) {
        String returnValue = "";
        if (date != null) {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
            returnValue = df.format(date);
        }
        return (returnValue);
    }

}
